package com.example.dishit.controller.createdish;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import android.content.Intent;
import android.provider.MediaStore;
import android.widget.Toast;

import com.example.dishit.controller.callback.IDishDataCallback;
import com.example.dishit.model.dbproxy.DBProxyFactory;
import com.example.dishit.model.dbproxy.IDBProxyCallback;
import com.example.dishit.model.dish.DishData;
import com.example.dishit.service.recommendation.RecommendationGenerator;
import com.example.dishit.view.createdish.CreateDishActivity;

public class CreateDishController implements ICreateDishController {
    public static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private static final long NEARBY_METERS = 100;
    private static final long VERY_NEARBY_METERS = 50;
    
   /**
    * (DBProxyUtils must be called from a single thread, otherwise this error can occur...
    *      "Make sure to release the connection before allocating another one.
    *      Invalid use of SingleClientConnManager: connection still allocated.")"
    */
    private final Executor locationServiceThread = Executors.newSingleThreadExecutor();
    
    private final CreateDishActivity view;
    
    public CreateDishController(CreateDishActivity view) { this.view = view; }
    
    public void getNearbyRestaurants(final IDishDataCallback callback) {
        // (Use a new thread each time [instead of just 1 for everyone].  That way,
        // if this thread ever locks-up, the system can still keep running.)
        locationServiceThread.execute(new Runnable() {
            public void run() {
                final List<DishData> restaurants = new ArrayList<DishData>();
                
                final List<DishData> veryNear = RecommendationGenerator.generateRecommendations(VERY_NEARBY_METERS);
                if(veryNear != null) restaurants.addAll(veryNear);
                
                final List<DishData> near = RecommendationGenerator.generateRecommendations(NEARBY_METERS);
                if(near != null) restaurants.addAll(near);
                
                callback.onDishDataCallback(restaurants);
            }
        });
    }
    
    public void cancel() { view.finish(); }
    
    public void sendDish(DishData data) {
        DBProxyFactory.instance().broadcastDish(data, new IDBProxyCallback() {
            public void onResultCallback(final boolean wasSuccessful, final String message) {
                view.runOnUiThread(new Runnable() {
                    public void run() {
                        if(wasSuccessful) {
                            Toast.makeText(view, "Dish was sent.", Toast.LENGTH_LONG).show();
                            view.finish();
                        } else {
                            Toast.makeText(view, "Unable to send this Dish: " + message, Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
            public void onDishDataCallback(List<DishData> dishDatas) {}
            public void onDishersCallback(List<String> myDishers) {}
        });
    }

    public void takePicture() {
        startCameraIntent();
    }
    
    private void startCameraIntent() {
        // Create Intent to take a picture and return control to the calling application.
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Start the image capture Intent.
        view.startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    }
}
